/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package cn.edu.thu.log.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * 
 * @author dongzihe
 *
 */
public class ParameterConstructor {
	private static Map<String, ArrayList<String>> productlistMap;

	static {
		productlistMap = new HashMap<String, ArrayList<String>>();
		ArrayList musicArray = new ArrayList<String>();
		musicArray.add("musicjump");
		musicArray.add("musicsearch");
		musicArray.add("musicclick");
		// musicArray.add("musictrans");
		productlistMap.put("music", musicArray);

		ArrayList pageArray = new ArrayList<String>();
		pageArray.add("pageclick");
		pageArray.add("pagesearch");
		pageArray.add("pagesnapshot");
		productlistMap.put("page", pageArray);
		ArrayList newsarray = new ArrayList<String>();
		newsarray.add("newsclick");
		newsarray.add("newssearch");
		// newsarray.add("newssub");
		productlistMap.put("news", newsarray);
		ArrayList imagearray = new ArrayList<String>();
		imagearray.add("imageclick");
		imagearray.add("imagesearch");
		productlistMap.put("image", imagearray);
		ArrayList transarray = new ArrayList<String>();
		transarray.add("webtrans");
		transarray.add("imagetrans");
		transarray.add("musictrans");
		productlistMap.put("transservice", transarray);
		ArrayList accarray = new ArrayList<String>();
		accarray.add("accbind");
		accarray.add("accbindweibo");
		// accarray.add("accesslog");
		accarray.add("accfindpwd");
		accarray.add("acchistoryop");
		accarray.add("acclogin");
		accarray.add("accreg");
		accarray.add("accverify");
		accarray.add("mypangupage");
		accarray.add("favLog");
		productlistMap.put("mypangu", accarray);
		ArrayList infoSend = new ArrayList<String>();
		infoSend.add("smssend");
		infoSend.add("msgtrace");
		productlistMap.put("infosend", infoSend);
		ArrayList navarray = new ArrayList<String>();
		navarray.add("navclick");
		// navarray.add("navonload");
		productlistMap.put("navigation", navarray);
		ArrayList clientarray = new ArrayList<String>();
		// clientarray.add("clientactive");
		clientarray.add("clientdownload");
		productlistMap.put("clientdownload", clientarray);
		ArrayList otherarray = new ArrayList<String>();
		otherarray.add("otherclick");
		otherarray.add("otherpage");
		productlistMap.put("other", otherarray);
		ArrayList realarray = new ArrayList<String>();

		realarray.add("realclick");
		realarray.add("realsearch");
		productlistMap.put("real", realarray);

	};
/**
 * 
 * @param startingDate parameter
 * @param endingDate parameter
 * @param productlist parameter
 * @return
 */
	public String inputPathConstructor(String startingDate, String endingDate,
			String productlist) {
		String inputpaths = "";
		String baseinputpath = "/logs/logbackup201206/";
		String[] products = productlist.split("/");
		DateFormat df = new SimpleDateFormat("yyyyMMddHH");
		DateFormat df2 = new SimpleDateFormat("yyyy/MM/dd");
		for (int i = 0; i < products.length; i++) {
			ArrayList<String> pl = productlistMap.get(products[i]);
			if (pl == null) {
				continue;
			}
			for (int j = 0; j < pl.size(); j++) {
				Date d1 = null;

				// d1.setHours(Integer.valueOf(this.startinghour));
				Date d2 = null;
				Date d3 = null;
				Date d4 = null;

				try {
					d1 = df2.parse(startingDate);
					d2 = df2.parse(endingDate);
					d3 = df2.parse("2012/06/01");
					d4 = df2.parse("2012/06/30");
					if( d1.getTime() < d3.getTime()) {
						d1 = d3;
					} 
					if( d1.getTime() > d4.getTime() ) {
						d2 = d4;
					}
					d1.setHours(0);
					d1.setMinutes(0);
				} catch (Exception e) {
					//System.out.println("");
				}
				// d2.setHours(Integer.valueOf(this.finishinghour));
				for (; d1.getTime() <= d2.getTime(); d1.setDate(d1.getDate()+1)) {
					String temppath = df.format(d1);
					// System.out.println(temppath);
					// System.out.println("i:"+i);
					String inputtemppath = "";
					inputtemppath = baseinputpath + pl.get(j) + "/"
							+ temppath+"00";
					if (!FilePathChecker.isExist(inputtemppath)) {
						continue;
					}
					if (inputpaths.equals("")) {
						inputpaths += inputtemppath;
					} else {
						inputpaths += "," + inputtemppath;
					}

				}
			}

		}
		return inputpaths;

	}
	
	public String inputPathConstructor2(String startingDate, String endingDate) {
		String inputpaths = "";
		String baseinputpath = "/logs/";		
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
		DateFormat df2 = new SimpleDateFormat("yyyy/MM/dd");
		Date d1 = new Date();
		Date d2 = new Date();
		Date d3 = new Date();
		Date d4 = new Date();
			try {
				d1 = df2.parse(startingDate);
				d2 = df2.parse(endingDate);
				d3 = df2.parse("2013/07/28");
				d4 = df2.parse("2013/07/29");
				if( d1.getTime() < d3.getTime()) {
						d1 = d3;
				} 
				if( d1.getTime() > d4.getTime() ) {
						d2 = d4;
				}
				d1=d3;
			} catch (Exception e) {
				//	System.out.println("");
			}
				// d2.setHours(Integer.valueOf(this.finishinghour));
			for (int kk = 0 ; kk < 10 ; d1.setMinutes(d1.getMinutes()+1),kk++) {
				String temppath = df.format(d1);
				String inputtemppath1 = "";
				String inputtemppath2 = "";
				inputtemppath1 = baseinputpath + "10.10.128.141" + "/logcenter."
							+ temppath+".log";
				inputtemppath2 = baseinputpath + "10.10.128.142" + "/logcenter."
						+ temppath+".log";
				
					if (FilePathChecker.isExist(inputtemppath1)) {
						if (inputpaths.equals("")) {
							inputpaths += inputtemppath1;
						} else {
							inputpaths += "," + inputtemppath1;
						}
				//		System.out.println(inputtemppath1);
					}
					if (FilePathChecker.isExist(inputtemppath2)) {
						if (inputpaths.equals("")) {
							inputpaths += inputtemppath2;
						} else {
							inputpaths += "," + inputtemppath2;
						}
					//	System.out.println(inputtemppath2);
					}				
				}			
		return inputpaths;
	}
	
/**
 * 
 * @param args parameter
 */
	public static void main(String[] args) {
		ParameterConstructor pc = new ParameterConstructor();
		System.out.println(pc.inputPathConstructor("2012/06/01", "2012/06/02",
				"other"));

	}

}
